package com.sunwayworld.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Title: DateFormat Description: manage the date and string Copyright:
 * Copyright (c) 2001 Company:
 * 
 * @author czc
 * @version 1.0
 */

public class DateFormat {

	private static String formatDate = "yyyy-MM-dd";

	private static String timePattern = formatDate + " HH:mm:ss";

	public static final String DATETIME = "yyyy-MM-dd HH:mm:ss";

	/**
	 * Description: 对该方法的调用只有第一次能有效;
	 * 
	 * @param str
	 * @return void
	 */

	/**
	 * use a private contructor to establish a singleton pattern
	 */
	private DateFormat() {
	}

	/**
	 * return current date in the specified format
	 */
	public static String currentDate(String format) {
		if (format == null)
			format = "";
		return formatDate(new Date(), format);
	}

	/**
	 * return date string in the specified format
	 */

	public static String formatDate(Date date) {
		return formatDate(date, formatDate);
	}

	public static String formatDate(Date date, String format) {
		if (date == null) {
			return "";
		}
		if (format == null)
			format = "";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	public static Date parseDate(String dateStr) {
		return parseDate(dateStr, formatDate);
	}

	/**
	 * parse the text to a date in the specified format
	 */
	public static Date parseDate(String dateStr, String format) {
		try {
			if (format == null)
				format = "";
			SimpleDateFormat sdf = new SimpleDateFormat(format);

			return sdf.parse(dateStr);

		} catch (Exception ex) {
			return null;
		}
	}

	/**
	 * parse the text to a date in the specified format add ntime
	 */
	public static Date parseDate(String dateStr, int ntime) {
		try {
			Date sdate = parseDate(dateStr);

			if (sdate == null) {
				return null;
			}
			Calendar clen = Calendar.getInstance(Locale.CHINESE);
			clen.setTime(sdate);
			clen.add(Calendar.DAY_OF_MONTH, ntime);
			sdate = clen.getTime();

			return sdate;

		} catch (Exception ex) {
			return null;
		}

	}

	// 显示时分秒格式的时间
	// YuMeng
	// 2005-8-16
	// param date: The date to be display in the pattern of "yyyy-mm-dd
	// hh:mm:ss"
	public static String formateDateTime(Date date) {
		if (date == null)
			return "";
		SimpleDateFormat sdf = new SimpleDateFormat(timePattern);

		return sdf.format(date);
	}

	// 显示时分秒格式的时间
	// YuMeng
	// 2005-8-16
	// param date: The date to be display in the pattern of "yyyy-mm-dd
	// hh:mm:ss"
	// param daysAdded: The days that are added to date.
	public static String formateDateTime(Date date, int daysAdded) {

		if (date == null)
			return "";
		Calendar cal = Calendar.getInstance(java.util.Locale.CHINA);
		cal.setTime(date);
		cal.add(Calendar.DATE, daysAdded);
		SimpleDateFormat sdf = new SimpleDateFormat(timePattern);
		return sdf.format(cal.getTime());

	}

	/**
	 * 
	 * @param dateStr
	 *            String
	 * @return Date
	 */
	public static Date parseDateTime(String dateStr) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(DATETIME);
			return sdf.parse(dateStr);
		} catch (Exception ex) {
			try {
				SimpleDateFormat sdf = new SimpleDateFormat(DATETIME);
				return sdf.parse(dateStr + " 00:00:00");
			} catch (Exception e) {
				return null;
			}
		}
	}
}
